Interact Exercises


In [ ]:
%matplotlib inline

In [ ]:
from matplotlib import pyplot as plt
import numpy as np

In [ ]:
from IPython.html.widgets import *
from IPython.display import display

String sorting

Write a sort_string function that takes a string as its input and prints a new string consisting of the original one, sorted. Add a reverse keyword argument with a default of False to allow for the sorting to be done in reverse.

Then, use interact to create a user interface for exploring your sort_string function.


In [ ]:
%load soln/string_sorting.py

Plotting with parameters

Write a plot_sin function that plots $sin(ax+b)$ over the interval $[0,4\pi]$.

Then use interact to create a user interface for exploring your function:

  • a should be a floating point number over the interval $[0.0,5.0]$.
  • b should be a floating point number over the interval $[-5.0,5.0]$.

In [ ]:
%load soln/param_plot_1.py

In matplotlib, the line style and color can be set with a third argument to plot. Examples of this argument:

  • dashed red: r--
  • blue circles: bo
  • dotted black: k.

Add a style argument to your plot_sin function that allows you to set the line style of the plot.

Use interact to create a UI for plot_sin that has a drop down menu for selecting the line style between a dotted red line and a dashed black line. This time use interact as a decorator.


In [ ]:
%load soln/param_plot_2.py

Simple data explorer

In this exercise, you will use interact to build a UI for exploring correlations between different features in the Iris dataset in [sklearn]http://scikit-learn.org/stable/(http://scikit-learn.org/stable/). This data contains 4 different measurements (called features in this content) of 150 different iris flowers of three different species.

Load the dataset:


In [ ]:
from sklearn.datasets import load_iris
iris_data = load_iris()

The actual data is stored as a NumPy array under the data attribute:


In [ ]:
iris_data.data.shape

You can see the meanings of the 4 columns of data by looking at the feature_names attribute:


In [ ]:
iris_data.feature_names

Write a plot_iris function that creates a scatter plot (using plt.scatter) of two columns of this dataset. Your function should have the following signature:

def plot_iris(a, col1, col2):
    ...

where a is the NumPy array of data and col1/col2 are the two columns to use for the scatter plot.

Use interact to build a UI to explore the iris dataset using your plot_iris function. You will need to use the fixed function when passing the dataset to the function.


In [ ]:
%load soln/data_explorer.py